package com.xjt.wx_admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjt.wx_admin.constant.WxConstant;
import com.xjt.wx_admin.entity.User;
import com.xjt.wx_admin.entity.UserApply;
import com.xjt.wx_admin.entity.UserFriend;
import com.xjt.wx_admin.entity.dto.ApplyUserParam;
import com.xjt.wx_admin.entity.dto.HandleApplyParam;
import com.xjt.wx_admin.entity.dto.SendMsg;
import com.xjt.wx_admin.mapper.UserApplyMapper;
import com.xjt.wx_admin.service.MsgRecordService;
import com.xjt.wx_admin.service.UserApplyService;
import com.xjt.wx_admin.service.UserFriendService;
import com.xjt.wx_admin.service.UserService;
import com.xjt.wx_admin.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Xujt
 * @since 2023-03-22 11:33:28
 */
@Service
public class UserApplyServiceImpl extends ServiceImpl<UserApplyMapper, UserApply> implements UserApplyService {

    @Autowired
    UserService userService;

    @Autowired
    UserFriendService userFriendService;

    @Autowired
    MsgRecordService msgRecordService;

    /**
     * 申请添加好友
     *
     * @param applyUserParam 申请添加好友参数
     * @return {@link R}
     */
    @Override
    public R applyUser(ApplyUserParam applyUserParam) {
        User user = userService.getById(applyUserParam.getApplyUserId());
        if (user==null){
            return R.failed("用户不存在，无法申请");
        }
        UserApply apply = getOne(Wrappers.<UserApply>lambdaQuery()
                .eq(UserApply::getUserId, SecurityUtils.getUserId())
                .eq(UserApply::getApplyId, applyUserParam.getApplyUserId())
                .eq(UserApply::getState, WxConstant.ApplyState.APPLY.getCode())
        );
        if (apply!=null){
            return R.failed("请勿重复申请");
        }
        UserApply userApply = new UserApply();
        userApply.setUserId(SecurityUtils.getUserId());
        userApply.setApplyMsg(applyUserParam.getMsg());
        userApply.setState(WxConstant.ApplyState.APPLY.getCode());
        userApply.setApplyId(applyUserParam.getApplyUserId());
        userApply.setApplyTime(LocalDateTime.now());
        userApply.setUpdateTime(LocalDateTime.now());
        return R.ok(save(userApply));
    }


    @Override
    public R handleApply(HandleApplyParam handleApplyParam) {
        UserApply apply = getOne(Wrappers.<UserApply>lambdaQuery()
                .eq(UserApply::getApplyId, SecurityUtils.getUserId())
                .eq(UserApply::getId, handleApplyParam.getId())
        );
        if (apply==null){
            return R.failed("申请记录不存在");
        }
        if (!apply.getState().equals(0)){
            return R.failed("重复处理");
        }
        apply.setState(handleApplyParam.getState());
        apply.setUpdateTime(LocalDateTime.now());
        updateById(apply);
//        同意
        if (handleApplyParam.getState().equals(1)){
//            互相添加好友关系
            UserFriend userFriend = new UserFriend();
            userFriend.setUserId(SecurityUtils.getUserId());
            userFriend.setFriendId(apply.getUserId());
            userFriendService.save(userFriend);

            UserFriend userFriend2 = new UserFriend();
            userFriend2.setUserId(apply.getUserId());
            userFriend2.setFriendId(SecurityUtils.getUserId());
            userFriendService.save(userFriend2);
//            默认发送一条消息
            msgRecordService.sendMsg(new SendMsg(apply.getUserId(),WxConstant.APPLY_MSG,WxConstant.MsgTextType.TEXT.getCode()));
            return R.ok("同意添加好友");
        }
        return R.ok("拒绝添加好友");
    }

    @Override
    public R getApplyList() {
        return R.ok(baseMapper.getApplyList(SecurityUtils.getUserId()));
    }
}
